$(document).ready(function(){
	//调用日期选择插件
    $('#datePicker').fdatepicker({
    	format: 'yyyy-mm-dd',
    });
  //发票方式单选
    $(".status").on("click",function(){
        $(this).addClass("on").siblings().removeClass("on");
    });
	var priceId = GetQueryString("priceId");
//	加载商品信息
	$.ajax({
		type:'POST',
		data:{'priceIds':priceId},
		url:path + "/orderlang/userCart/findListByPriceIds",
		success:function(data){
			console.log(data);
			if(data.data){
				var list = data.data;
				var html = "";
				for(var i = 0;i<list.length;i++){
					html += "<tr data-priceId='"+list[i].productPriceId+"'data-id='"+list[i].productId+"'>";
					html += "<td>";
					html += "<div class='fl'>";
					html += "<img src='/docs/"+list[i].productImg+"' alt=''/>";
					html += "</div>";
					html += "<div class='detail fl'>";
					html += "<p>";
					html += "<span>p351585115</span>";
					html += "</p>";
					html += "<p>";
					html += "<span>"+list[i].productName+"</span>";
					html += "</p>";
					html += "</div>";
					html += "</td>";
					html += "<td>";
					for(var j = 0;j<list[i].specList.length;j++){
	                	html += "<span>"+list[i].specList[j].specName+"："+list[i].specList[j].specValue+"；</span>";
	                }
					html += "</td>";
					html += "<td class='markerPrice'>"+cc((list[i].marketPrice).toString())+"</td>";
					html += "<td class='price' data-price='"+list[i].discountPrice+"'>"+cc((list[i].discountPrice).toString())+"</td>";
					html += "<td class='count'>";
					html += "<span>"+list[i].buyQuantity+"</span>";
					html += "</td>";
					html += "<td class='subtotal'><span data-money='"+list[i].discountPrice*list[i].buyQuantity+"'>"+cc((list[i].discountPrice*list[i].buyQuantity).toString())+"</span></td>";
					html += "<td>";
					html += "<p>移除</p>";
					html += "<p>加入我的收藏</p>";
					html += "</td>";
					html += "<td></td>";
					html += "</tr>";
				}
				$('.product-item').find('table').html(html);
				var subTotal = 0;
				$('.product-item .subtotal').each(function(){
					subTotal += Number($(this).find('span').attr('data-money'));
				})
				$('.totalMoney').html(cc(subTotal.toString())).attr('data-money',subTotal);
			}
		},
		error:function(){
			
		}
	});
//	获取收货人信息
	$.ajax({
		type:'POST',
		url:path + "/orderlang/userAddress/list/all",
		success:function(data){
			$('.address').attr('data-id',data[0].id);
			$(".name").html(data[0].consignee);
			$(".phone").html(data[0].mobile);
			$(".addr").html(data[0].address);
		},
		erroe:function(){
			
		}
	})
	//提交订单
	$('.check').click(function(){
		var orderMsg = {};
		var productList = [];
		$('.product-item tr').each(function(){
			var product = {};
			product.productId = $(this).attr('data-id');
			product.productPriceId = $(this).attr('data-priceId');
			product.price = $(this).find('.price').attr('data-price');
			product.quantity = $(this).find('.count').find('span').html();
			product.amount = $(this).find('.subtotal').find('span').attr('data-money');
			productList.push(product);
		});
		orderMsg.productList = productList;
		orderMsg.addressId = $('.address').attr('data-id');
		orderMsg.deliveryDateTemp = $('#datePicker').val();
		orderMsg.invoiceType = $('.bill').children('.on').find('span').html();
		orderMsg.remark = $('.remark').find('input').val();
		orderMsg.amount = $('.totalMoney').attr('data-money');
		console.log(JSON.stringify(orderMsg));
		if(orderMsg.deliveryDateTemp == ""){
			layer.msg("请选择发货日期", {icon: 2,time:1000});
		}else{
			$.ajax({
				type:'POST',
				data:{'orderMsg':JSON.stringify(orderMsg)},
				url:path + '/orderlang/order/saveOrderAndProduct',
				success:function(data){
					console.log(data);
					if(data.state == 1){
						window.location.href = path + "/orderlang/order/goPayRecord?id=" + data.data.id +"&orderCode=" + data.data.orderCode +"&amount=" + data.data.amount;
					}
				},
				errot:function(){
					
				}
			})
		}
	 });
});

